package com.github.dengmin.mysql.controller;

import com.github.dengmin.mysql.dto.LoginDto;
import com.github.dengmin.mysql.model.ApiResponse;
import com.github.dengmin.mysql.service.LoginService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author dengmin
 * @Created 2020/6/28 下午1:49
 */
@RestController
@RequestMapping("/auth")
public class AuthController {
    private static final long serialVersionUID = 1L;

    @Resource
    private LoginService loginService;

    @PostMapping("/login")
    public ApiResponse<Object> login(@Validated LoginDto login) throws Exception{
        String token = loginService.login(login.getUsername(), login.getPassword());
        return ApiResponse.ok(token);
    }

    @GetMapping("/logout")
    public ApiResponse<Object> logout(HttpServletRequest request) throws Exception{
        loginService.logout(request);
        return ApiResponse.ok("");
    }

}
